package jeecg.business.controller.setting;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.setting.NmsSystemSettingEntity;
import jeecg.business.service.setting.NmsSystemSettingServiceI;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 系统设置
 * @author LinYingPiaoJian
 * @date 2015-06-11 21:57:09
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/nmsSystemSettingController")
public class NmsSystemSettingController extends BaseController {
	
	public static String UNID = "1";
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NmsSystemSettingController.class);

	@Autowired
	private NmsSystemSettingServiceI nmsSystemSettingService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 系统设置列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "nmsSystemSetting")
	public ModelAndView nmsSystemSetting(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/setting/nmsSystemSettingList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(NmsSystemSettingEntity nmsSystemSetting,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(NmsSystemSettingEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, nmsSystemSetting);
		this.nmsSystemSettingService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除系统设置
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(NmsSystemSettingEntity nmsSystemSetting, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		nmsSystemSetting = systemService.getEntity(NmsSystemSettingEntity.class, nmsSystemSetting.getId());
		message = "删除成功";
		nmsSystemSettingService.delete(nmsSystemSetting);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加系统设置
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(NmsSystemSettingEntity nmsSystemSetting, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(nmsSystemSetting.getId())) {
			message = "更新成功";
			NmsSystemSettingEntity t = nmsSystemSettingService.get(NmsSystemSettingEntity.class, nmsSystemSetting.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(nmsSystemSetting, t);
				nmsSystemSettingService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			nmsSystemSetting.setId(UNID);
			nmsSystemSettingService.save(nmsSystemSetting);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 系统设置列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(NmsSystemSettingEntity nmsSystemSetting, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(nmsSystemSetting.getId())) {
			nmsSystemSetting = nmsSystemSettingService.getEntity(NmsSystemSettingEntity.class, nmsSystemSetting.getId());
			req.setAttribute("nmsSystemSettingPage", nmsSystemSetting);
		}
		return new ModelAndView("jeecg/business/setting/nmsSystemSetting");
	}
}
